<template>
  <div v-if="isVisible" class="modal-overlay">
    <div class="modal-content">
      <div class="modal-header">
        <div class="modal-header-left">
          <img src="../../../assets/images/home-page/eq-right.png" alt="" />
          <span>保存数据</span>
        </div>
        <img @click="closeModal" src="../../../assets/images/home-page/close-bt.png" alt="" />
      </div>
      <div class="m-c-box">
        <div class="distance">
          <span>测量时间</span>
          <span>{{ formattedDate }}</span>
        </div>
        <div class="distance">
          <span>测量名称</span>
          <input type="text" placeholder="请输入…" v-model="toSave" />
        </div>
        <div class="m-c-btn">
          <button class="btn-close" @click="closeModal">取消</button>
          <button class="btn" @click="preservation">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { ref, onMounted } from 'vue';
  import { NstdMessage } from 'nstd-components-pc-v3';
  const emit = defineEmits();
  const props = defineProps(['isVisible']);
  const toSave = ref('');
  const formattedDate = ref('');

  onMounted(() => {
    const now = new Date();
    formattedDate.value =
      now.getFullYear() +
      '-' +
      ('0' + (now.getMonth() + 1)).slice(-2) +
      '-' +
      ('0' + now.getDate()).slice(-2) +
      ' ' +
      ('0' + now.getHours()).slice(-2) +
      ':' +
      ('0' + now.getMinutes()).slice(-2) +
      ':' +
      ('0' + now.getSeconds()).slice(-2);
  });
  function toggleModal() {
    isVisible.value = !isVisible.value;
  }

  function openModal() {
    isVisible.value = true;
  }
  function preservation() {
    if (toSave.value === '') {
      NstdMessage.error('请输入测量名称');
      return;
    }
    emit('preservation', toSave.value);
  }
  function closeModal() {
    emit('closeModal');
  }
</script>

<style lang="scss" scoped>
  .modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .modal-content {
    width: 330px;
    background: rgba(5, 30, 67, 0.6);
    .modal-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: rgba(5, 30, 67, 0.8);
      width: 100%;
      height: 34px;
      .modal-header-left {
        display: flex;
        align-items: center;
        margin-left: 8px;
        img {
          width: 20px;
          height: 20px;
          margin-right: 10px;
        }
        span {
          font-family: Microsoft YaHei;
          font-weight: 400;
          font-size: 14px;
          color: #ffffff;
        }
      }
      .close-button {
        cursor: pointer;
        font-size: 20px;
      }
      img {
        width: 16px;
        height: 16px;
        margin-right: 20px;
        &:hover {
          cursor: pointer;
          opacity: 0.85;
        }
      }
    }
    .m-c-box {
      width: 100%;
      height: 20vh;
      border-radius: 4px;
      .distance {
        display: flex;
        align-items: center;
        margin: 30px 10px;
        span {
          font-family: Microsoft YaHei;
          font-weight: 400;
          font-size: 14px;
          color: #ffffff;
          margin-left: 20px;
        }
        input {
          width: 200px;
          height: 30px;
          background: transparent;
          border: 1px solid RGBA(116, 152, 168, 1);
          border-radius: 4px;
          padding-left: 10px;
          color: #ffffff;
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          margin-left: 20px;
          outline: none;
        }
      }
      .m-c-btn {
        width: 42%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 0 auto;
        .btn-close {
          width: 60px;
          height: 30px;
          background: #21427c;
          border-radius: 4px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          font-size: 14px;
          color: #ffffff;
          border: none;
          &:hover {
            opacity: 0.85;
            cursor: pointer;
          }
        }
        .btn {
          width: 60px;
          height: 30px;
          background: linear-gradient(0deg, #0b8ccf, #07268a);
          border-radius: 4px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          font-size: 14px;
          color: #ffffff;
          border: none;
          &:hover {
            opacity: 0.85;
            cursor: pointer;
          }
        }
      }
    }
  }
</style>
